package com.igeek.ch02.service;

import com.igeek.ch02.dao.UserDao;
import com.igeek.ch02.entity.User;

import java.sql.SQLException;

/**
 * @author wangpeng
 * 2023/7/25 15:54
 * @description 业务逻辑层
 */
public class UserService {

    private UserDao userDao = new UserDao();

    //登录
    public User login(String username , String address){
        try {
            User user = userDao.findOne(username, address);
            return user;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //对连接归还操作
        }
        return null;
    }

    //注册
    public boolean register(User user){
        boolean flag = this.validateName(user.getUsername());
        if(flag){
            try {
                int i = userDao.insert(user);
                return i>0?true:false;
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    //校验姓名是否存在
    public boolean validateName(String username){
        try {
            User user = userDao.findOne(username);
            if(user!=null){
                //名字被占用，不可用
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //名字未被占用，可用
        return true;
    }

    //分页条件查询列表
}
